/*
 * @(#)DateTableCellRenderer.java	11/06/02
 * Copyleft 2011. All rights reversed.
 *
 * This file is part of AverageGrade.
 *
 * AverageGrade is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * AverageGrade is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AverageGrade. If not, see <http://www.gnu.org/licenses/>.
 */

package net.grade.averagegrade.swing.table;

import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * A cell renderer which formats its values as dates, using the format 'yyyy-MM-dd'
 * and english locale. Only formats <code>Date</code> objects and uses the default
 * rendering provided by <code>DefaultTableCellRenderer</code> for all other objects.
 *
 * @see Date
 * @see SimpleDateFormat
 */
public class DateTableCellRenderer extends DefaultTableCellRenderer {

    /** The common <code>DateFormat</code> used for formatting <code>Date</code> objects. */
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

    @Override
    public Component getTableCellRendererComponent(final JTable table, Object value,
            final boolean isSelected, final boolean hasFocus, final int row, final int column) {

        if(value != null && value instanceof Date)
            value = dateFormat.format((Date)value);

        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}